package web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import pojo.Brand;
import service.brandService.brandService;
import service.brandServiceImpl.brandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/selectByIdServlet")
public class selectByIdServlet extends HttpServlet {
    private brandService brandservice=new brandServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader br = req.getReader();//获取请求体数据（id）
        String s = br.readLine();
        int id=Integer.parseInt(s);//转化类型
        Brand brand = brandservice.selectById(id);
        String jsonString = JSON.toJSONString(brand, SerializerFeature.IgnoreErrorGetter);//转化为json对象
        resp.setContentType("text/json;charset=utf-8");//防止中文乱码
        resp.getWriter().write(jsonString);//响应数据

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
